package com.weipai.education.service.impl;

import com.weipai.education.service.FileUploadService;
import com.weipai.education.utils.FileUploadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/10 17:11
 */
@Service
public class FileUploadServiceImpl implements FileUploadService {

    private final static Logger logger = LoggerFactory.getLogger(FileUploadServiceImpl.class);


    @Override
    public String saveFileUpload(HttpServletRequest request, MultipartFile file) {
        String uploadStatus = null;
        try{
            uploadStatus = FileUploadUtil.ImgSave(request, file);
        } catch (Exception e) {
            logger.error("服务器内部错误{}", e);
            uploadStatus = "error";
        }
        return uploadStatus;
    }

    @SuppressWarnings("static-access")
    @Override
    public String saveImgUpload(HttpServletRequest request , MultipartFile file) {
        String uploadStatus = null;
        try {
            uploadStatus = FileUploadUtil.ImgSave(request, file);
            //uploadStatus = yuanFilePath+newImgName;
        } catch (Exception e) {
            logger.error("服务器内部错误", e);
            uploadStatus = "error";
        }
        return uploadStatus;
    }

    @Override
    public String saveImgUpload(HttpServletRequest request, MultipartFile[] image) {
        StringBuffer sb = new StringBuffer();
        String a = "";
        if(image!=null&&image.length>0) {
            for (int i = 0; i < image.length; i++) {
                MultipartFile img =  image[i];
                a = saveImgUpload(request,img);
                if(image.length==1)
                    sb.append(a);
                else
                    sb.append(a+",");
            }
        }
        return new String(sb.substring(0,sb.length()-1));
    }
}
